//
//  SmsCloudDataHelper.m
//  smsCloud
//
//  Created by 吴斌 on 2024/7/17.
//

#import "SmsCloudDataHelper.h"
#import "SmsCloudNetManger.h"
@implementation SmsCloudDataHelper
static SmsCloudDataHelper *defaultMag = nil;
+(SmsCloudDataHelper *)defaultMag{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        if(defaultMag == nil){
            defaultMag = [[self alloc] init];
        }
    });
    return defaultMag;
}
+ (void)requestSmsCloudPrice{
    __weak __typeof__(self) weakSelf = self;
    NSDictionary *parmDic = @{@"packageName":[SmsCloudHead SmsCloudPublicAppBundId]};
    [[SmsCloudNetManger defaultMag] smsCloudNetPost:[SmsCloudHead SmsCloudUrlVipPrice] withParam:parmDic withSuccess:^(id  _Nonnull data) {
        NSDictionary * dic = [data objectForKey:@"data"];
        [SmsCloudDataHelper defaultMag].smsCloudPrice = [dic objectForKey:@"price"];
    } withFail:^{
        [weakSelf requestSmsCloudPrice];
    }];
}
+ (void)requestSmsCloudVipMd{
    __weak __typeof__(self) weakSelf = self;
    if ([SmsCloudDataHelper defaultMag].smsCloudPrice.length > 0) {
        NSString *udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        NSDictionary *parmDic = @{@"name":@"sms",@"price":[SmsCloudDataHelper defaultMag].smsCloudPrice,@"udid":udid};
        [[SmsCloudNetManger defaultMag] smsCloudNetPost:[SmsCloudHead SmsCloudUrlMD] withParam:parmDic withSuccess:^(id  _Nonnull data) {} withFail:^{
            [weakSelf requestSmsCloudVipMd];
        }];
    }
}
+ (void)requestSmsCloudProductList:(void(^)(void))scuessBlock{
    __weak __typeof__(self) weakSelf = self;
    [[SmsCloudNetManger defaultMag] smsCloudNetGet:[SmsCloudHead SmsCloudProductList] withParam:@{} withSuccess:^(id  _Nonnull data) {
        NSMutableArray * datas = [NSMutableArray arrayWithArray:[data objectForKey:@"data"]];
        for (int i = 0; i < datas.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:datas[i]];
            if (i < 3) {
                int random = arc4random_uniform(202) + 200;
                [dic setObject:[NSString stringWithFormat:@"%d",random] forKey:@"num"];
            }else{
                int random = arc4random_uniform(131) + 50;
                [dic setObject:[NSString stringWithFormat:@"%d",random] forKey:@"num"];
            }
            [datas replaceObjectAtIndex:i withObject:dic];
        }
        [SmsCloudDataHelper defaultMag].smsCloudProducts = [SmsCloudProductModel mj_objectArrayWithKeyValuesArray:datas];
        
        scuessBlock();
    } withFail:^{
        [weakSelf requestSmsCloudProductList:scuessBlock];
    }];
}
+ (void)requestSmsCloudCity:(NSString *)cityCode scuessBlock:(void(^)(void))scuessBlock{
    NSMutableDictionary *parmDic = [[NSMutableDictionary alloc] init];
    [parmDic setObject:cityCode forKey:@"service"];
    [SVProgressHUD show];
    [[SmsCloudNetManger defaultMag] smsCloudNetPost:[SmsCloudHead SmsCloudCityList] withParam:parmDic withSuccess:^(id  _Nonnull data) {
        NSArray * datas = [data objectForKey:@"data"];
        [SmsCloudDataHelper defaultMag].smsCloudCitys = [SmsCloudCityModel mj_objectArrayWithKeyValuesArray:datas];
        scuessBlock();
        [SVProgressHUD dismiss];
    } withFail:^{
        [SVProgressHUD dismiss];
    }];
}
+ (void)requestSmsCloudRedomPhone:(NSString *)cityCode withProductCode:(NSString *)productCode scuessBlock:(void(^)(SmsCloudPhoneModel *smsCloudPhoneMdl))scuessBlock{
    [SVProgressHUD show];
    NSMutableDictionary *parmDic = [[NSMutableDictionary alloc] init];
    [parmDic setObject:productCode forKey:@"service"];
    [parmDic setObject:cityCode forKey:@"country"];
    [parmDic setObject:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"udid"];
    [[SmsCloudNetManger defaultMag] smsCloudNetPost:[SmsCloudHead SmsCloudRedomPhone] withParam:parmDic withSuccess:^(id  _Nonnull data) {
        NSDictionary * dataDic = [data objectForKey:@"data"];
        SmsCloudPhoneModel *smsCloudPhoneMdl = [SmsCloudPhoneModel mj_objectWithKeyValues:dataDic];
        scuessBlock(smsCloudPhoneMdl);
        [SVProgressHUD dismiss];
    } withFail:^{
        [SVProgressHUD dismiss];
    }];
}
+ (void)requestSmsCloudPhoneCode:(NSString *)cityCode withPhone:(NSString *)phone withProductCode:(NSString *)productCode scuessBlock:(void(^)(NSString *smsCloudPhone))scuessBlock{
    NSMutableDictionary *parmDic = [[NSMutableDictionary alloc] init];
    [parmDic setObject:productCode forKey:@"service"];
    [parmDic setObject:cityCode forKey:@"country"];
    [parmDic setObject:phone forKey:@"phone"];
    [[SmsCloudNetManger defaultMag] smsCloudNetPost:[SmsCloudHead SmsCloudPhoneCode] withParam:parmDic withSuccess:^(id  _Nonnull data) {
        id dataObject = [data objectForKey:@"data"];
        if([dataObject isKindOfClass:[NSArray class]]){
            NSArray * phoneCodes = dataObject;
            scuessBlock(phoneCodes.firstObject);
        }else if([dataObject isKindOfClass:[NSDictionary class]]){
            NSDictionary * phoneCodeDic = dataObject;
            if ([phoneCodeDic.allKeys containsObject:@"smsCode"]) {
                NSArray *phoneCodes = [phoneCodeDic objectForKey:@"smsCode"];
                scuessBlock(phoneCodes.firstObject);
            }else{
                scuessBlock([phoneCodeDic objectForKey:@"smsCode"]);
            }
        }else{
            scuessBlock(dataObject);
        }
    } withFail:^{
        
    }];
}
@end
